//
//  ZZTabBar.swift
//  网易微博
//
//  Created by gazyy1 on 16/6/25.
//  Copyright © 2016年 gazyy1. All rights reserved.
//

import UIKit

//代理声明方法
protocol ZZTabBarDelegate : NSObjectProtocol {
    func centerButtonClick()
}


class ZZTabBar: UITabBar {
    
    //创建代理
    var zzDelegate : ZZTabBarDelegate?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //添加中间button
        addUI()

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    //自定义tabBar
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //设置tabBar的宽度
        let valueW = self.bounds.width / 5
        //定义标示
        var index : CGFloat = 0
        
        //遍历子控件设置属性
        for value in subviews {
        
            if value.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                
                value.frame.origin.x = index * valueW
                value.frame.size.width = valueW
                
                index++
                
                if index == 2 {
                    index++
                }
                
            }
        }
        centerButton.center.x = self.frame.width * 0.5
        centerButton.center.y = self.frame.height * 0.5
        
    }
    
    //添加button
    private func addUI(){
        addSubview(centerButton)
        
    }
    
    
    
    //定义中间的按钮
    lazy var centerButton : UIButton = {
      
        let button = UIButton()
        button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.sizeToFit()
        
        return button
    }()
    
    //按钮点击事件
   @objc private func buttonClick(){
    
    zzDelegate?.centerButtonClick()
    
    }
    
    
    
}
